﻿using _1._2._2015_MVCTodo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _1._2._2015_MVCTodo.Controllers
{
    public class TodoController : Controller
    {
        private TodoService _service;

        public TodoController()
        {
            _service = TodoService.Instance;
        }

        // GET: get all todo items
        [HttpGet]
        public ActionResult Index()
        {
            TodoIndexViewModel model = new TodoIndexViewModel();

            model.Todos = _service.GetTodos();

            return View(model);
        }

        // GET: create new todo item
        [HttpGet]
        public ActionResult Create()
        {
            CreateTodoViewModel model = new CreateTodoViewModel();

            return View(model);
        }

        // POST: create new todo item
        [HttpPost]
        public ActionResult Create(CreateTodoViewModel model)
        {
            int id = _service.CreateTodo(model.Text);

            return RedirectToAction("Index");
        }

        // GET: update todo item
        [HttpGet]
        public ActionResult Update(int id)
        {
            UpdateTodoViewModel model = new UpdateTodoViewModel();

            TodoViewModel todo = _service.GetTodo(id);
            model.IsCompleted = todo.IsCompleted;
            model.Id = todo.Id;
            model.Text = todo.Text;

            return View(model);
        }

        // POST: update todo item
        [HttpPost]
        public ActionResult Update(UpdateTodoViewModel model)
        {
            _service.UpdateTodo(model.Id, model.Text, model.IsCompleted);

            return RedirectToAction("Index");
        }
    }
}